package cn.edu.neusoft.food729.model;


import cn.edu.neusoft.food729.Bean.UserBean;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.iface.Useriface;
import cn.edu.neusoft.food729.listener.UserListener;
import cn.edu.neusoft.food729.service.UserByIdService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserModel implements Useriface {

    private Retrofit retrofit;

    public UserModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList(int user_id, final UserListener userListener) {
        UserByIdService userByIdService
                = retrofit.create(UserByIdService.class);
        Call<UserBean> call = userByIdService.getUserById(user_id);

        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.isSuccessful() && response.body() != null)
                    userListener.onResponse(response.body());
                else userListener.onFail("on response fail");
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                userListener.onFail(t.getMessage());
                System.out.println("--网络错误：" + t.getMessage());
            }
        });
    }
}
